﻿
namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
    using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
    using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
    using Huirui.Cavan.Domain.Seedwork;
    using Huirui.Cavan.Presentation.Seedwork;
    using Huirui.Cavan.Presentation.ViewModel.SystemModule;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using System.Linq;
    using AutoMapper;
    using Newtonsoft.Json;

    public class UserAccountController : EntityController<UserAccount, UserAccountSearch, IUserAccountViewModel, IUserAccountAppService>, IUserAccountController
    {
        private IRoleAppService RoleAppService { get; set; }
        private IEmployeeAppService EmployeeAppService { get; set; }

        public UserAccountController()
        {
            this.Model = ModelFactory.CreateModel<IUserAccountViewModel>();
        }

        #region properties
        public override string ControllerName
        {
            get { return ControllerConst.USERACCOUNT; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }
        #endregion properties

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "useraccount_query")]
        public ActionResult AddUserAccount()
        {
            return View("~/Views/System/v1/addUserAccount.cshtml");
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "useraccount_edit")]
        public JsonResult AddUserAccount(UserAccountViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.IsLocked = model.IsLocked == "1" ? true : false;
            BoolGenericResult<UserAccount> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult() { IsSuccess = result.IsSuccess, Messages = result.Messages };
            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.LIST)]
        [ActionPermissionAttribute(Name = "useraccount_query")]
        public ActionResult GetUserAccounts()
        {
            IList<Employee> employees = this.EmployeeAppService.GetBySearch(new EmployeeSearch() { CorporationCode = SecurityAppService.Corporation.Id, HasUserAccount = true }).ToList();
            UserAccountJsonViewModel json = new UserAccountJsonViewModel();
            json.rows = new List<UserAccountJson>();

            foreach (Employee employee in employees)
            {
                UserAccountJson uaj = Mapper.Map<UserAccount, UserAccountJson>(employee.UserAccount);
                json.rows.Add(uaj);
            }
            json.total = employees.Count;

            return Json(json, "application/json");
        }

        //[ActionName(ActionConst.DELETE)]
        //public JsonResult DeleteUserAccount(string selected)
        //{
        //    BoolGenericResult<UserAccount> result = null;
        //    List<UserAccountJson> userAccounts = JsonConvert.DeserializeObject<List<UserAccountJson>>(selected);

        //    foreach (UserAccountJson json in userAccounts)
        //    {
        //        UserAccount ua = this.AppService.GetById(json.Id);
        //        ua.IsDeleted = true;
        //        result = this.AppService.Update(ua);
        //    }

        //    BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

        //    return Json(jsonResult, "application/json");
        //}
    
    }
}
